#include <game/ScreenBackendFactory.h>
#include <game/ScreenBackend.h>
#include <Gluck.h>

ARELLO_GAME_NAMESPACE_DECL_START

ScreenBackendFactory::ScreenBackendFactory()
{
}

ScreenBackendFactory::ScreenBackendFactory(const ScreenBackendFactory& rhs)
	: creators(rhs.creators)
{
}

ScreenBackendFactory& ScreenBackendFactory::operator=(const ScreenBackendFactory& rhs)
{
	this->creators = rhs.creators;
	return *this;
}

ScreenBackendFactory::~ScreenBackendFactory()
{
}

std::auto_ptr<ScreenBackend> ScreenBackendFactory::CreateBackend(const std::string& name)
{
	CreatorMap::const_iterator i = this->creators.find(name);
	if (i == this->creators.end())
		return std::auto_ptr<ScreenBackend>();
	
	// Invoke backend creator.
	return i->second();
}

void ScreenBackendFactory::RegisterBackendCreator(const std::string& name, const BackendCreator& creator)
{
	std::pair<CreatorMap::iterator, bool> insertResult = this->creators.insert(make_pair(name, creator));
	if (!insertResult.second)
	{
		std::string description = "Tried to register more than one backend creator with the name \"";
		description += name;
		description += "\"";
		THROW(Gluck(__FUNCTION__, description));
	}
}

ARELLO_GAME_NAMESPACE_DECL_END